package com.harboursoftware.xstorage.ac;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.harboursoftware.xstorage.model.XUser;

/**
 * 用户授权对象，每个CanonicalUserGrantee实例表示一个特定的用户。
 * @author Simon Leung
 * @since 0.1
 */
public class CanonicalUserGrantee implements Grantee {
    private String id;
    private String displayName;
    
    public CanonicalUserGrantee() {
        
    }
    
    public CanonicalUserGrantee(String id, String displayName) {
        this.id = id;
        this.displayName = displayName;
    }
    
    public boolean contains(XUser user) {
        if(user==null || user.getId()==null) {
            return false;
        }
        if(user.getId().equals(id)) {
            return true;
        }
        return false;
    }
    
    public String toXML() {
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("<Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"CanonicalUser\">");
        strBuilder.append("<ID>");
        strBuilder.append(StringUtils.defaultString(id, ""));
        strBuilder.append("</ID>");
        strBuilder.append("<DisplayName>");
        strBuilder.append(StringUtils.defaultString(displayName, ""));
        strBuilder.append("</DisplayName>");
        strBuilder.append("</Grantee>");
        return strBuilder.toString();
    }
    
    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }
    
	@Override
    public String toString() {
	    return ToStringBuilder.reflectionToString(this);
    }

    /**
     * the scope is 1.
     * @return 1
     */
    public int getScope() {
        return 1;
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getDisplayName() {
        return displayName;
    }
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

}
